//
//  EditView2Controller.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 15/11/24.
//  Copyright © 2015年 sgone. All rights reserved.
//

#import "EditView2Controller.h"


#import "AFNetworking.h"
#import "MJRefresh.h"
#import "QiniuSDK.h"
#import "MBProgressHUD+MJ.h"
#import "Fruits2.h"
#import "Fruits1.h"
//添加地址区域
#import "HZAreaPickerView.h"
#import "UIImageView+AFNetworking.h"
//新的SDWebImage
#import "UIImageView+WebCache.h"

#import "PreviewViewController.h"
#define NUMBERS @"0123456789.\n"

#define imageH  180


@interface EditView2Controller ()<UITextFieldDelegate,UITextViewDelegate,UIActionSheetDelegate,UIPickerViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIPickerViewDataSource,HZAreaPickerDelegate,UITextViewDelegate,UIScrollViewDelegate>



@property(nonatomic,strong) UIScrollView *scrollView;
//@property(nonatomic,strong) UIView *view4;


@property(nonatomic,strong) UITextField *textName;
@property(nonatomic,strong) UITextField *textFenlei;
@property(nonatomic,strong) UITextField *textChandi;
@property(nonatomic,strong) UITextField *textNumber;
@property(nonatomic,strong) UITextField *textUnit;
@property(nonatomic,strong) UITextField *textHeavy;
@property(nonatomic,strong) UITextField *textPrice;
@property(nonatomic,strong) UITextField *textJunjia;
//@property(nonatomic,strong) UITextField *textKucun;
@property(nonatomic,strong) UITextField *textphone;
@property(nonatomic,strong) UITextView *textDescribe;
@property(nonatomic,strong) UITextField *textShangjia;








//规格
@property(nonatomic,strong) NSString *guiGe;

//网路请求
@property(nonatomic,strong)AFHTTPSessionManager *manager;


////上传七牛服务器
@property (nonatomic, strong) QNUploadManager *uploader;
//获得七牛的token的数据
@property(nonatomic,strong) NSString *QNtoken;

//上传到七牛服务器所进行的拼接字符串
@property (nonatomic, copy) NSString *nameKey;
//上传到七牛形成的网络路径
@property (copy, nonatomic)NSString *filePath;
//设置显示的图片
@property (nonatomic,strong) UIImageView *imageView;
//获取的得到image的URL,
@property(nonatomic,strong) NSString *productImage;



//获得分类pickView获得的处理
//生成一个PickView工具
@property(nonatomic,strong) UIPickerView *pickView1;
//蒙版
@property(nonatomic,strong) UIView *orderMskView;
@property(nonatomic,strong) NSMutableArray *orderDatas;
@property(nonatomic) NSInteger currentSelectInCom0;
//上传到服务器的ID2的子的分类号码
@property(nonatomic,strong) NSString *ID2;
//上传服务器的分类名称
@property(nonatomic,strong) NSString *productName;

//地址信息
@property (strong,nonatomic) NSString *areaValue;

//获取得到省市
@property (nonatomic,strong)NSString *stateValue;
@property (strong,nonatomic)NSString *cityValue;


@property (strong,nonatomic) HZAreaPickerView *locatePicker;
//添加城市pickView蒙版
@property (strong,nonatomic) UIView *orderMskView2;



//value将值传递到服务器,需要判断上下架1上架 0下架,上下架操作
//状态值的传递
@property(nonatomic,strong) NSString *value;
@property(nonatomic,strong) UIPickerView *pickView2;
//蒙版3
@property(nonatomic,strong) UIView *orderMskView3;
//设置上下架状态的数据源操作
@property(nonatomic,strong)  NSArray *shangJiaArray;





//textView label的待位符
@property(nonatomic,strong) UILabel *placeholder;



//获得分类的catgory以及product
@property(nonatomic,strong) NSString *category;
@property(nonatomic,strong) NSString *product;


@end


@implementation EditView2Controller



//七牛上传图片的状态成功或者是失败
{
    
    //imagePickerView进行处理
    
    UIImagePickerController *_ctrl;
    
    
    
    //地址pickView处理
    NSString *_areaValue;
    NSString *_cityValue;
    HZAreaPickerView *_locatePicker;
    //商品上下架
    UIButton *btnShang;
    UIButton *btnXia;
    
}





//隐藏tabbar
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
        
    }
    return self;
}



//afnetworking网络请求数据
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        _manager  = [AFHTTPSessionManager manager];
    }
    return _manager;
    
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titleLabel.text = @"编辑商品";
    self.backBtn.hidden = NO;
    //上下架操作
    self.shangJiaArray = [NSArray arrayWithObjects:@"上架",@"下架",nil];
    self.orderDatas = [[NSMutableArray alloc]initWithCapacity:0];
    self.currentSelectInCom0 = 0;
    self.scrollView  = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, ViewWidth, ViewHeight-64 - 64 )];
    self.scrollView.contentSize = CGSizeMake(ViewWidth, 680+34);
    self.scrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scrollView];
    self.scrollView.scrollEnabled = YES;
    self.scrollView.bounces = YES;//到底部弹回到最后
    self.scrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
    self.scrollView.delegate = self;
    
    
    
    //赋予ID分类的初始值
    self.ID2 = [NSString stringWithFormat:@"%@",[self.listDic objectForKey:@"product_id"]];
    self.view.backgroundColor = BACKGROUND1;
//    添加底部button界面操作
    [self initBottom];
    //获取七牛token
    [self getQiniuToken];
    
    
    //异步加载数据
    dispatch_async(dispatch_get_main_queue(), ^{
        //获取分类信息
        
        [self getFenlei];
        
    });
    
    
    
    [self initData];
//    [self setBack];
}

- (void)setBack {
    
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 44, 44)];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateHighlighted];
    [self.topView addSubview:backBtn];
}

- (void)back {
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"  message:@"未保存修改,确定退出？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
    [alert show];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 1) {
        
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

#pragma mark 设置底部bottom
-(void)initBottom

{
    self.view.backgroundColor = [UIColor blackColor];
    UIButton *proBtn = [[UIButton alloc]initWithFrame:CGRectMake(0,ViewHeight -54 , 120, 54)];
    [proBtn setTitle:@"预  览" forState:UIControlStateNormal];
    proBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [proBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:proBtn];
    [proBtn addTarget:self action:@selector(pro:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(120, ViewHeight - 64, ViewWidth-120, 64)];
    btn2.backgroundColor = BenSe_COLOR;
    [self.view addSubview:btn2];
    [btn2 setTitle:@"确   认" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn2.layer.cornerRadius = 1.0;
    [btn2 addTarget:self action:@selector(sure:) forControlEvents:UIControlEventTouchUpInside];
    

    
}





//填充数据
-(void)initData
{

    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, imageH , ViewWidth, 10)];
    view1.backgroundColor = BACKGROUND1;
    [self.scrollView addSubview:view1];
    
    
    UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(15, imageH+10, 100, 43)];
    //    lbl1.backgroundColor = [UIColor greenColor];
    [self.scrollView addSubview:lbl1];
    lbl1.text = @"商品名";
    lbl1.textColor = [UIColor grayColor];
    [self shu:0];
    
    
    
    
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(15, imageH + 10 +43, ViewWidth-15, 1)];
    view2.backgroundColor = BACKGROUND1;
    [self.scrollView addSubview:view2];
    
    
    UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(15, imageH+10+44, 100, 43)];
    //    lbl2.backgroundColor = [UIColor greenColor];
    [self.scrollView addSubview:lbl2];
    lbl2.text = @"   分类";
    lbl2.textColor = [UIColor grayColor];
    [self shu:1];
    
    
    
    
    
    
    
    
    UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(15, imageH + 10 +43+44, ViewWidth-15, 1)];
    view3.backgroundColor = BACKGROUND1;
    [self.scrollView addSubview:view3];
    
    UILabel *lbl3 = [[UILabel alloc]initWithFrame:CGRectMake(15, imageH+10+44*2, 100, 43)];
    //    lbl2.backgroundColor = [UIColor greenColor];
    [self.scrollView addSubview:lbl3];
    lbl3.text = @"   单价";
    lbl3.textColor = [UIColor grayColor];
     [self shu:2];
    
    
    
    UIView *view4 = [[UIView alloc]initWithFrame:CGRectMake(15, imageH + 10 +43+44*2, ViewWidth-15, 1)];
    view4.backgroundColor = BACKGROUND1;
    [self.scrollView addSubview:view4];
    
    
    UILabel *lbl4 = [[UILabel alloc]initWithFrame:CGRectMake(15, imageH+10+44*3, 100, 43)];
    //    lbl2.backgroundColor = [UIColor greenColor];
    [self.scrollView addSubview:lbl4];
    lbl4.text = @"   均价";
    lbl4.textColor = [UIColor grayColor];
    [self shu:3];
    
    
    
    
    UIView *view44 = [[UIView alloc]initWithFrame:CGRectMake(15, imageH + 10 +43+44*3, ViewWidth-15, 1)];
    view44.backgroundColor = BACKGROUND1;
    [self.scrollView addSubview:view44];
    UILabel *lbl44 = [[UILabel alloc]initWithFrame:CGRectMake(15, imageH+10+44*4, 100, 43)];
    //    lbl2.backgroundColor = [UIColor greenColor];
    [self.scrollView addSubview:lbl44];
    lbl44.text = @"   产地";
    lbl44.textColor = [UIColor grayColor];
    [self shu:4];

    
    
    UIView *view5 = [[UIView alloc]initWithFrame:CGRectMake(15, imageH + 10 +43+44*4, ViewWidth-15, 1)];
    view5.backgroundColor = BACKGROUND1;
    [self.scrollView addSubview:view5];
    
    UILabel *lbl5 = [[UILabel alloc]initWithFrame:CGRectMake(15, imageH+10+44*5, 100, 43)];
    //    lbl2.backgroundColor = [UIColor greenColor];
    [self.scrollView addSubview:lbl5];
    lbl5.text = @"   规格";
    lbl5.textColor = [UIColor grayColor];
    [self shu:4];
    
    
    
    UIView *view6 = [[UIView alloc]initWithFrame:CGRectMake(15, imageH + 10 +43+44*5, ViewWidth-15, 1)];
    view6.backgroundColor = BACKGROUND1;
    [self.scrollView addSubview:view6];
    
    UILabel *lbl6 = [[UILabel alloc]initWithFrame:CGRectMake(15, imageH+10+44*6, 100, 43)];
    //    lbl2.backgroundColor = [UIColor greenColor];
    [self.scrollView addSubview:lbl6];
    lbl6.text = @"联系电话";
    lbl6.textColor = [UIColor grayColor];
    [self shu:5];
    
    
/*
    UIView *view7 = [[UIView alloc]initWithFrame:CGRectMake(15, imageH + 10 +43+ 44*5, ViewWidth-15, 1)];
    view7.backgroundColor = BACKGROUND1;
    [self.scrollView addSubview:view7];
    
    
    UILabel *lbl7 = [[UILabel alloc]initWithFrame:CGRectMake(15, imageH+10+44*6, 100, 43)];
    //    lbl2.backgroundColor = [UIColor greenColor];
    [self.scrollView addSubview:lbl7];
    lbl7.text = @"联系电话";
    lbl7.textColor = [UIColor grayColor];
    
*/
    
    
    
    
    
    
    UIView *view8 = [[UIView alloc]initWithFrame:CGRectMake(15, imageH + 10 +44+ 44* 6, ViewWidth-15, 1)];
    view8.backgroundColor = BACKGROUND1;
    [self.scrollView addSubview:view8];
    [self shu:6];

    
//    UILabel *lbl8 = [[UILabel alloc]initWithFrame:CGRectMake(15, imageH+10+44*6+28, 100, 43)];
//    [self.scrollView addSubview:lbl8];
//    lbl8.text = @"商品描述";
//    lbl8.textColor = [UIColor grayColor];
    
    
    
    //添加上下架状态
    
    
//    UIView *view10 = [[UIView alloc]initWithFrame:CGRectMake(15, imageH + 10 +44+ 44* 6 +115, ViewWidth-15, 1)];
//    view10.backgroundColor = BACKGROUND1;
//    [self.scrollView addSubview:view10];
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    /*
    //添加上下架状态显示上下架
    self.textShangjia = [[UITextField alloc]initWithFrame:CGRectMake(115, imageH + 10 +43+ 44*6 +115, ViewWidth *0.7, 44)];
//    self.textShangjia.backgroundColor = [UIColor redColor];
    [self.scrollView addSubview:self.textShangjia];
    
    
    self.textShangjia.font = [UIFont systemFontOfSize:16];
    self.textShangjia.textColor = [UIColor lightGrayColor];
    self.textShangjia.placeholder = @"请点击选择上下架状态";
    [self.scrollView addSubview:self.textShangjia];
    self.textShangjia.delegate = self;
    self.textShangjia.returnKeyType = UIReturnKeyDone;
    self.textShangjia.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    
    //不可编辑进行操作
    self.textShangjia.userInteractionEnabled = NO;
    */
    
    
    
    
    
    
    
    UIView *view9 = [[UIView alloc]initWithFrame:CGRectMake(15, imageH + 10 +43*7 +120, ViewWidth-15, 1)];
    view9.backgroundColor = BACKGROUND1;
    [self.scrollView addSubview:view9];
    
    
    self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake((ViewWidth-imageH)/2, 0, imageH , imageH )];
//    self.imageView.image = [UIImage imageNamed:@"addpro-1"];
    [self.scrollView addSubview:self.imageView];
    
    
    UIButton *btnImage = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, imageH)];
    

    [btnImage addTarget:self action:@selector(imagePick:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:btnImage];
    UIButton *btnImage2 = [[UIButton alloc]initWithFrame:CGRectMake(ViewWidth * 0.8, imageH -70, 60, 60)];
//    btnImage2.backgroundColor = [UIColor greenColor];
    
    [btnImage2 setImage:[UIImage imageNamed:@"btnImage3"] forState:UIControlStateNormal];
    [self.scrollView addSubview:btnImage2];
    [btnImage2 addTarget:self action:@selector(imagePick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //实例化textField
    
    
    self.textName = [[UITextField alloc]initWithFrame:CGRectMake(115,imageH+10 , ViewWidth *0.6, 44)];
    self.textName.font = [UIFont systemFontOfSize:16];
    self.textName.textColor = [UIColor lightGrayColor];
    self.textName.placeholder = @"输入商品名称";
    [self.scrollView addSubview:self.textName];
    
    self.textName.delegate = self;
    //显示键盘变为完成
    self.textName.returnKeyType = UIReturnKeyDone;
    //cleanButton
    self.textName.clearButtonMode =  UITextFieldViewModeWhileEditing;
    
    
    
    
    self.textFenlei = [[UITextField alloc]initWithFrame:CGRectMake(115, imageH + 10 +43, ViewWidth *0.6, 44)];
    
    self.textFenlei.font = [UIFont systemFontOfSize:16];
    self.textFenlei.textColor = [UIColor lightGrayColor];
    self.textFenlei.placeholder = @"添加商品分类";
    [self.scrollView addSubview:self.textFenlei];
    
    self.textFenlei.userInteractionEnabled = NO;
    self.textFenlei.delegate = self;
    //显示键盘变为完成
    self.textFenlei.returnKeyType = UIReturnKeyDone;
    
    
    
    //添加button键
    UIButton *btnFL = [[UIButton alloc]initWithFrame:CGRectMake(115, imageH + 10 +43, ViewWidth *0.7, 44)];
    btnFL.backgroundColor = [UIColor clearColor];
    
    [self.scrollView addSubview:btnFL];
    [btnFL addTarget:self action:@selector(pickerView1FL:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    
    
    
    
    
    

    self.textChandi = [[UITextField alloc]initWithFrame:CGRectMake(115, imageH + 10 +43+44*3, ViewWidth *0.6, 44)];
    
    self.textChandi.font = [UIFont systemFontOfSize:16];
    self.textChandi.textColor = [UIColor lightGrayColor];
    self.textChandi.placeholder = @"请选择商品产地";
    [self.scrollView addSubview:self.textChandi];
    self.textChandi.userInteractionEnabled = NO;
    self.textChandi.delegate = self;
    self.textChandi.returnKeyType = UIReturnKeyDone;
    
    
    //赋值操作
    self.textChandi.text = _areaValue;
    
    
    UIButton *btnCD = [[UIButton alloc]initWithFrame:CGRectMake(115, imageH + 10 +43+44*3, ViewWidth *0.7, 44)];
    btnCD.backgroundColor = [UIColor clearColor];
    [self.scrollView addSubview:btnCD];
    
    
    [btnCD addTarget:self action:@selector(pickView2CD:) forControlEvents:UIControlEventTouchUpInside];
 
    
    
    
    
    
    
    
    self.textNumber = [[UITextField alloc]initWithFrame:CGRectMake(110, imageH + 10 +43+44*4+4, 60, 36)];
    self.textNumber.delegate = self;
    self.textNumber.returnKeyType = UIReturnKeyDone;
    [self.scrollView addSubview:self.textNumber];
    self.textNumber.placeholder = @"数量";
    self.textNumber.keyboardType = UIKeyboardTypeNumberPad;
    self.textNumber.font = [UIFont systemFontOfSize:16];
    self.textNumber.textColor = [UIColor lightGrayColor];
    //设置textField的边框
    self.textNumber.layer.borderWidth = 1.0f;
    self.textNumber.layer.cornerRadius = 5;
    self.textNumber.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.textNumber.textAlignment = NSTextAlignmentCenter;
    self.textNumber.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    
    
    
    
    self.textUnit = [[UITextField alloc]initWithFrame:CGRectMake(110+60+10, imageH + 10 +43+44*4+4, 60, 36)];
    
    self.textUnit.delegate = self;
    self.textUnit.returnKeyType = UIReturnKeyDone;
    [self.scrollView addSubview:self.textUnit];
    self.textUnit.placeholder = @"单位";
    self.textUnit.font = [UIFont systemFontOfSize:16];
    self.textUnit.textColor = [UIColor lightGrayColor];
    
    //设置textField的边框
    self.textUnit.layer.borderWidth = 1.0f;
    self.textUnit.layer.cornerRadius = 5;
    self.textUnit.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.textUnit.textAlignment = NSTextAlignmentCenter;
    self.textUnit.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    
    
    
    
    self.textHeavy = [[UITextField alloc]initWithFrame:CGRectMake(110+60*2+10*2, imageH + 10 +43+44*4+4, 60, 36)];
    
    self.textHeavy.delegate = self;
    self.textHeavy.returnKeyType = UIReturnKeyDone;
    [self.scrollView addSubview:self.textHeavy];
    self.textHeavy.placeholder = @"约重量";
    self.textHeavy.font = [UIFont systemFontOfSize:16];
    self.textHeavy.textColor = [UIColor lightGrayColor];
    //设置textField的边框
    self.textHeavy.layer.borderWidth = 1.0f;
    self.textHeavy.layer.cornerRadius = 5;
    self.textHeavy.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.textHeavy.textAlignment = NSTextAlignmentCenter;
    self.textHeavy.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    
    self.guiGe =  [NSString stringWithFormat:@"%@ %@ %@",self.textNumber.text,self.textUnit.text, self.textHeavy.text];
    
    
    
    
    self.textPrice = [[UITextField alloc]initWithFrame:CGRectMake(115, imageH + 10 +43+44, ViewWidth *0.6, 44)];
    
    self.textPrice.font = [UIFont systemFontOfSize:16];
    self.textPrice.textColor = [UIColor lightGrayColor];
    self.textPrice.placeholder = @"请输入单价";
    [self.scrollView addSubview:self.textPrice];
    self.textPrice.delegate = self;
    self.textPrice.returnKeyType = UIReturnKeyDone;
    self.textPrice.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    
    
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth *0.6 + 110, imageH + 10 +43+44, 20, 44)];
    lbl.font = [UIFont systemFontOfSize:15];
    lbl.text = @"元";
    lbl.textColor = [UIColor grayColor];
    [self.scrollView addSubview:lbl];
    
    
    
    
    self.textJunjia  = [[UITextField alloc]initWithFrame:CGRectMake(115, imageH + 10 +43+44*2, ViewWidth *0.5, 44)];
    
    self.textJunjia.font = [UIFont systemFontOfSize:16];
    self.textJunjia.textColor = [UIColor lightGrayColor];
    self.textJunjia.placeholder = @"如: 5元/斤";
    [self.scrollView addSubview:self.textJunjia];
    self.textJunjia.delegate = self;
    self.textJunjia.returnKeyType = UIReturnKeyDone;
    self.textJunjia.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    
    UILabel *lbl10 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth *0.5 + 110+12, imageH + 10 +43+44*2, 48, 44)];
    lbl10.font = [UIFont systemFontOfSize:15];
    lbl10.text = @"元/斤";
    lbl10.textColor = [UIColor grayColor];
    [self.scrollView addSubview:lbl10];
    
    
    
    
    self.textphone  = [[UITextField alloc]initWithFrame:CGRectMake(115, imageH + 10 +43+44*5, ViewWidth *0.6, 44)];
    
    self.textphone.font = [UIFont systemFontOfSize:16];
    self.textphone.textColor = [UIColor lightGrayColor];
    self.textphone.placeholder = @"请输入手机号";
    [self.scrollView addSubview:self.textphone];
    self.textphone.delegate = self;
    self.textphone.returnKeyType = UIReturnKeyDone;
    self.textphone.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    self.textphone.text = self.dataHandler.phone;
    
    
    
    
    self.textDescribe = [[UITextView alloc]initWithFrame:CGRectMake(10, imageH + 10 +45+44*6, ViewWidth - 10* 2, 110)];
    
    [self.scrollView addSubview:self.textDescribe];
    
    
    self.textDescribe.font = [UIFont systemFontOfSize:16];
    self.textDescribe.textColor = [UIColor lightGrayColor];
    
    self.placeholder  = [[UILabel alloc]initWithFrame:CGRectMake(10, imageH + 10 +45+44*6, 300, 30)];
    self.placeholder.font = [UIFont systemFontOfSize:15];
    self.placeholder.textColor = [UIColor lightGrayColor];
    self.placeholder.text = @"  描述一下商品详情吧,详细送货时间等~";
    [self.scrollView addSubview:self.placeholder];
    
    self.textDescribe.delegate = self;
    //将键盘变成完成操作
    self.textDescribe.returnKeyType = UIReturnKeyDone;
    
    
    
    
    
    
    UILabel *lbl9 = [[UILabel alloc]initWithFrame:CGRectMake(15, imageH+10+44*7 +115, 60, 43)];
    [self.scrollView addSubview:lbl9];
    lbl9.text = @"  上下架";
    lbl9.textColor = [UIColor grayColor];
    
    //商品上下架
    btnShang = [[UIButton alloc]initWithFrame:CGRectMake(115, self.textDescribe.frame.origin.y + self.textDescribe.frame.size.height + 12, 50, 30)];
    [btnShang setTitle:@"上架" forState:UIControlStateNormal];
    [self.scrollView addSubview:btnShang];
    btnShang.titleLabel.font = [UIFont systemFontOfSize:14];
    [btnShang setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [btnShang setTitleColor:BenSe_COLOR forState:UIControlStateSelected];
    [btnShang setImage:[UIImage imageNamed:@"xiaN"] forState:UIControlStateNormal];
    [btnShang setImage:[UIImage imageNamed:@"shangH"] forState:UIControlStateSelected];
    [btnShang addTarget:self action:@selector(zt:) forControlEvents:UIControlEventTouchUpInside];
   
    
    btnShang.layer.masksToBounds = YES;
    btnShang.layer.cornerRadius = 4.0;
    btnShang.layer.borderWidth = 1.0;
    
    
    
    btnXia = [[UIButton alloc]initWithFrame:CGRectMake(115 + 50 + 20, self.textDescribe.frame.origin.y + self.textDescribe.frame.size.height+12, 50, 30)];
    [btnXia setTitle:@"下架" forState:UIControlStateNormal];
    [self.scrollView addSubview:btnXia];
    btnXia.titleLabel.font = [UIFont systemFontOfSize:14];
    [btnXia setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [btnXia setTitleColor:BenSe_COLOR forState:UIControlStateSelected];
    [btnXia setImage:[UIImage imageNamed:@"xiaN2"] forState:UIControlStateNormal];
    [btnXia setImage:[UIImage imageNamed:@"xiaH"] forState:UIControlStateSelected];
    [btnXia addTarget:self action:@selector(zt:) forControlEvents:UIControlEventTouchUpInside];

    
    btnXia.layer.masksToBounds = YES;
    btnXia.layer.cornerRadius = 4.0;
    btnXia.layer.borderWidth = 1.0;

    
    
    //判断上下架状态
    
    if ([[self.listDic objectForKey:@"status"] intValue]  == 0) {
        
//        self.textShangjia.text = @"下架";
        self.value = @"0";
        btnShang.selected = NO;
        btnXia.selected = YES;
        
        btnShang.layer.borderColor =[UIColor grayColor].CGColor;
        btnXia.layer.borderColor = BenSe_COLOR.CGColor;
    }
    else
    {
        
        
//        self.textShangjia.text = @"上架";
        self.value = @"1";
        btnShang.selected = YES;
        btnXia.selected = NO;
        btnShang.layer.borderColor = BenSe_COLOR.CGColor;
        btnXia.layer.borderColor = [UIColor grayColor].CGColor;
        
    }
    
    
    
//    NSLog(@"  iiiiiii%@  %@",self.value,[self.listDic objectForKey:@"status"]);
    

    
    
    
/*
    //添加上下架状态的按钮显示pickView
    UIButton * btnSJ = [[UIButton alloc]initWithFrame:CGRectMake(115, imageH + 10 +43+ 44*6 +115, ViewWidth *0.7, 44)];
    
    
    
    //    btnSJ.backgroundColor = [UIColor redColor];
    [self.scrollView addSubview:btnSJ];
    
    [btnSJ addTarget:self action:@selector(shangjia:) forControlEvents:UIControlEventTouchUpInside];
 */
    
    
    
    //显示网络图片
    
    if ([[self.listDic objectForKey:@"image"] isKindOfClass:[NSNull class]] || [[self.listDic objectForKey:@"image"] length] == 0 ) {
        self.imageView.image = [UIImage imageNamed:@"backI"];
    }
    
    
    else
    
    {
    
    NSURL *url = [ NSURL URLWithString:[self.listDic objectForKey:@"image"]];

    [self.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"backImage"]];
     
    self.imageView.backgroundColor = [UIColor greenColor];
        
        
        //初始照片的地址
        
        //截取图片字符串,截取下标为30开始之后的字符串
        NSString *imageURL = [[self.listDic objectForKey:@"image"] substringFromIndex:31];
        self.nameKey = imageURL;
    
    }
    
    
    
    
    //给生成的textField赋值
    
    self.textName.text =   [NSString stringWithFormat:@"%@",[self.listDic objectForKey:@"product_name"]];
    self.textFenlei.text = [NSString stringWithFormat:@"%@ %@",[self.listDic objectForKey:@"category"],[self.listDic objectForKey:@"product"]];
    
    
    
    //category和product.初始赋值
    self.category = [NSString stringWithFormat:@"%@",[self.listDic objectForKey:@"category"]];
    self.product =[NSString stringWithFormat:@"%@",[self.listDic objectForKey:@"product"]];
    
    
    
    
    self.textChandi.text = [NSString stringWithFormat:@"%@%@",[self.listDic objectForKey:@"province"],[self.listDic objectForKey:@"city"]];
    
    
    
    if (self.textChandi.text.length != 0 && ![self.textChandi.text isEqualToString:@""])
    {
       
        //初始城市地址
        self.stateValue =  [NSString stringWithFormat:@"%@",[self.listDic objectForKey:@"province"]];
        self.cityValue =  [NSString stringWithFormat:@"%@",[self.listDic objectForKey:@"city"]];
        
    }
    

    
    
    
    
    //获得商品的规格
    NSString *str = [NSString stringWithFormat:@"%@",[self.listDic objectForKey:@"product_unit"]];
    NSString *temp  = @"";
    
    
    
    
    self.textNumber.font = [UIFont systemFontOfSize:14];
    self.textUnit.font = [UIFont systemFontOfSize:14];
    self.textHeavy.font = [UIFont systemFontOfSize:14];
    
    for ( int i = 0; i < str.length; i++) {
        
        temp = [str substringWithRange:NSMakeRange(i, 1)];
        //空格处理
        if ([temp isEqualToString:@" "]) {
            
            NSArray *array2 = [str componentsSeparatedByString:@" "];
            //用来存放处理后的字符串
            self.textNumber.text = array2[0];
            self.textUnit.text = array2[1];
            self.textHeavy.text = array2[2];
            
            
        }
        else
        {
            
            
            
        }
        
    }
    
    self.guiGe =  [NSString stringWithFormat:@"%@ %@ %@",self.textNumber.text,self.textUnit.text, self.textHeavy.text];
    
    self.textPrice.text = [NSString stringWithFormat:@"%@",[self.listDic objectForKey:@"unit_price"]];
    
    self.textJunjia.text = [NSString stringWithFormat:@"%@",[self.listDic objectForKey:@"avg_price"]];
    self.textphone.text = [NSString stringWithFormat:@"%@",[self.listDic objectForKey:@"tel"]];
    self.textDescribe.text = [NSString stringWithFormat:@"%@",[self.listDic objectForKey:@"description"]];

    
    
    
    
    //判断需要显示textDescribe的label
    
//    NSLog(@"%@哈吉斯大恒科技阿什顿哈SD卡会尽快圣诞节卡萨   ",self.textDescribe.text);

    if (self.textDescribe.text.length == 0 || [self.textDescribe.text isKindOfClass:[NSNull class]]) {
        
        
        self.placeholder.hidden = NO;

        
        
    }
    else
    {
    
        self.placeholder.hidden = YES;
    
    }
    
  
    
}


-(void)zt:(UIButton *)sender
{
    btnShang.selected = btnXia.selected = NO;
    if ([sender.titleLabel.text isEqualToString:@"上架"]) {
        
        sender.selected = YES;
        self.value = @"1";
        btnShang.layer.borderColor = BenSe_COLOR.CGColor;
        btnXia.layer.borderColor = [UIColor grayColor].CGColor;
        
    }
    else if ([sender.titleLabel.text isEqualToString:@"下架"])
    {
        sender.selected = YES;
        self.value = @"0";
        btnShang.layer.borderColor = [UIColor grayColor].CGColor;
        btnXia.layer.borderColor = BenSe_COLOR.CGColor;
        
    }
    
    
    
}


-(UIView *) shu:(int)a
{
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(90, imageH+10+10+44*a, 1, 23)];
    view2.backgroundColor = BACKGROUND1;
    [self.scrollView addSubview:view2];
    return view2;
}
    
    

#pragma mark 商品分类pickView的实现


-(void)pickerView1FL:(id)sender
{
    
    
    self.pickView1.hidden = NO;
    self.orderMskView.hidden = NO;
    
    [self.view endEditing:YES];
    
    [UIView animateWithDuration:0.25 animations:^{
        if (self.view.y == -176) {
            self.view.y = 0;
        }
    }completion:^(BOOL finished){
        
        self.scrollView.scrollEnabled = YES;
        
    }];
    
    Fruits1*select1 = _orderDatas[0];
    Fruits2*select2 = select1.fruits1[0];
    
    NSString *str = [NSString stringWithFormat:@"%@ %@",select1.fruits1Name,select2.fruits2Name];
    self.textFenlei.text = str;
}



#pragma mark 商品产地的实现

-(void)pickView2CD:(id)sender
{
    
    [self.view endEditing:YES];
    
    [UIView animateWithDuration:0.25 animations:^{
        if (self.view.y == -176) {
            self.view.y = 0;
        }
    }completion:^(BOOL finished){
        
        self.scrollView.scrollEnabled = YES;
        
    }];
    
    if (self.orderMskView2 == nil)
    {
        
        self.orderMskView2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)];
        self.orderMskView2.backgroundColor = [UIColor clearColor];
        [self.view addSubview:self.orderMskView2];
        
        
        
        //收回pickView操作
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pickViewHide:)];
        
        //设置成NO便是当前空间响应后会传播到其他控件上，默认为YES
        tapGes.cancelsTouchesInView = NO;
        [self.orderMskView2 addGestureRecognizer:tapGes];
        
    }
    
    
    
    
    
    
    if (self.locatePicker == nil) {
        
        self.locatePicker = [[HZAreaPickerView alloc]initWithStyle:HZAreaPickerWithStateAndCityAndDistrict delegate:self];
        
//        self.locatePicker.frame = CGRectMake(0, ViewHeight *0.7, ViewWidth, ViewHeight*0.3);
        self.locatePicker.frame = CGRectMake(0, ViewHeight-230, ViewWidth, 230);
        
        [self.orderMskView2 addSubview:self.locatePicker];
        
//        [self.locatePicker showInView:self.orderMskView2];
        
        
        
    }
    self.orderMskView2.hidden = NO;
    self.locatePicker.hidden = NO;
    
    self.areaValue = [NSString stringWithFormat:@"北京市 通州区"];
    
    //重新赋值
    self.stateValue = @"北京市";
    self.cityValue = @"通州区";
    
    
    //    产地重新赋值
    self.textChandi.text = self.areaValue;
}





#pragma mark 显示上下架的pickView的生成
-(void)shangjia:(id)sender
{

    
    if (self.orderMskView3 == nil)
    {
        
        self.orderMskView3 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)];
        self.orderMskView3.backgroundColor = [UIColor greenColor];
        [self.view addSubview:self.orderMskView3];
        //收回pickView操作
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(shangjiaa:)];
        
        //设置成NO便是当前空间响应后会传播到其他控件上，默认为YES
        tapGes.cancelsTouchesInView = NO;
        [self.orderMskView3 addGestureRecognizer:tapGes];
        
    }
    
    /*
    if (self.pickView2 == nil) {
        
        
        self.pickView2 = [[UIPickerView alloc]initWithFrame:CGRectMake(0,ViewHeight - 64- 65, ViewWidth, 128)];
        self.pickView2.delegate = self;
        
        [self.pickView2 setTag:105];
        
        int j = 0;
        for (int i = 0; i < self.shangJiaArray.count; i ++) {
            
            if ([self.textShangjia.text isEqualToString:self.shangJiaArray[i]]) {
                
                j = i;
            }
        }
        
        [self.pickView2 selectRow:j inComponent:0 animated:NO];
        
        self.pickView2.backgroundColor = [UIColor whiteColor];
        [self.orderMskView3 addSubview:self.pickView2];
    }
    
    */
    self.orderMskView3.hidden = NO;
//    self.pickView2.hidden = NO;
    
}



#pragma mark 上架状态的pickView以及蒙版隐藏
-(void)shangjiaa:(id)sender
{

    [self.orderMskView3 setHidden:YES];
    [self.pickView2 setHidden:YES];
    
    

}





#pragma mark 预览
-(void)pro:(id)sender
{
    
    
    if (self.nameKey.length == 0 || self.textName.text.length == 0 || self.textFenlei.text.length == 0 || self.textNumber.text.length == 0 || self.textUnit.text.length == 0 || self.textHeavy.text.length == 0 || self.textPrice.text.length == 0 || self.textJunjia.text.length == 0  || self.textphone.text.length == 0 || self.textDescribe.text.length == 0) {
        
        
        [self.view makeToast:@"请完善商品信息"];
        return;
    }
    
    
    
    
    self.guiGe =  [NSString stringWithFormat:@"%@ %@ %@",self.textNumber.text,self.textUnit.text, self.textHeavy.text];
    PreviewViewController *preView = [[PreviewViewController alloc]initWithNibName:nil bundle:nil];
    
    
    
    //传值
    preView.nameKey = [NSString stringWithFormat:@"http://fruitsimage.qiniudn.com/%@",self.nameKey];
    
    preView.textName = self.textName.text;
    preView.textFenlei = self.textFenlei.text;
    preView.textChandi = self.textChandi.text;
    
    preView.guiGe = self.guiGe;
    preView.textPrice = self.textPrice.text;
    preView.textJunjia  = self.textJunjia.text;
    //    preView.textKucun = self.textKucun.text;
    preView.textphone = self.textphone.text;
    preView.textDescribe = self.textDescribe.text;
    
    
    
    
    [self.navigationController pushViewController:preView animated:YES];
    
    
    
}







    

#pragma mark 确定，上传数据到服务器上
-(void) sure:(id)sender
{
    
    
    
    
    
    if ([[self.listDic objectForKey:@"image"] isKindOfClass:[NSNull class]]  || [[self.listDic objectForKey:@"image"] length] == 0)
    {
        
        [self.view makeToast:@"后台MM忘记上传商铺图片啦"];
        
        return;
        
    }
    
    

    
    
    if (self.nameKey.length == 0) {
        
        [self.view makeToast:@"请上传商品照片"];
        return;
    }
    
    if (self.textName.text.length == 0) {
        [self.view makeToast:@"名称不能为空"];
        return;
    }
    if (self.textFenlei.text.length == 0) {
        
        [self.view makeToast:@"分类不能为空"];
        
        return;
    }
    if (self.textChandi.text.length == 0) {
        [self.view makeToast:@"产地不能为空"];
        return;
    }
    if (self.textNumber.text.length == 0) {
        [self.view makeToast:@"数量不能为空"];
        return;
    }
    if (self.textUnit.text.length == 0) {
        [self.view makeToast:@"单位不能为空"];
        return;
    }
    if (self.textHeavy.text.length == 0) {
        [self.view makeToast:@"重量不能为空"];
        return;
    }
    if (self.textPrice.text.length == 0) {
        [self.view makeToast:@"单价不能为空"];
        return;
    }
    if (self.textJunjia.text.length == 0) {
        [self.view makeToast:@"均价不能为空"];
        return;
    }
    if (self.textphone.text.length == 0) {
        [self.view makeToast:@"联系电话不能为空"];
        return;
    }
    if (self.textDescribe.text.length == 0) {
        [self.view makeToast:@"描述不能为空"];
        return;
    }
    
    if (![self.value isEqualToString:@"0"]  && ![self.value isEqualToString:@"1"]) {
        
        
        [self.view makeToast:@"商品上下架状态不能为空"];
        return;
        
    }
    
    
    
 
        
        
    
    

    
    
//    NSLog(@"whwhwhwhhwhwhh    %ld %@  %@  " ,self.textChandi.text.length ,self.nameKey,self.productImage);
    
    
    //规格
    
    self.guiGe =  [NSString stringWithFormat:@"%@ %@ %@",self.textNumber.text,self.textUnit.text, self.textHeavy.text];

    
//    NSLog(@"333333333a  %@ %@   %@ %@   %@  %@  %@  %@   %@  %@ %@  %@   %@",self.textName.text,self.textPrice.text,self.textChandi.text,self.textJunjia.text,self.guiGe,self.cityValue,self.stateValue,self.guiGe, self.ID2,self.textFenlei.text,self.stateValue ,self.cityValue,self.nameKey);
//    self.cityValue = @"";
//    self.stateValue = @"";
    
    NSString*urlStr = [NSString stringWithFormat:@"%@update_product",SERVER_ADDRESS2];
    
    NSDictionary *params = @{@"wTel" : self.dataHandler.phone , @"uToken" : self.dataHandler.uToken ,@"product_id" : self.ID2, @"product_name" : self.textName.text , @"unit_price" : self.textPrice.text , @"product_unit" : self.guiGe ,@"avg_price" : self.textJunjia.text , @"category" : self.category , @"product" :  self.product , @"province" :self.stateValue , @"city" : self.cityValue  , @"image" : self.nameKey  ,@"tel" :  self.textphone.text  ,@"description" : self.textDescribe.text , @"status" : self.value };
    
    [MBProgressHUD showHUDAddedTo:self.scrollView animated:YES];
    
    
    [self.manager POST:urlStr parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject[@"error_flag"] intValue]  == 0) {
            
//            NSLog(@"上传商品成功");
            
            
            [MBProgressHUD hideHUDForView:self.scrollView animated:YES];
            
            
        
            
            
            //判断修改上传成功进行上一个页面跳转
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"xiugai" object:nil];
            
            
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"上传成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
            [self.navigationController popViewControllerAnimated:YES];
            
            
            
        }
        else
        {
            
            
            [MBProgressHUD hideHUDForView:self.scrollView animated:YES];
            NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
            
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
            
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [MBProgressHUD hideHUDForView:self.scrollView animated:YES];
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"上传服务器失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alter show];
        
        
    }];
    
    
    
    
}





    
    
    



#pragma  mark获取七牛的token


//通过七牛上传图片
- (void)getQiniuToken
{
    
    //    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    //    [MBProgressHUD showMessage:@"加载中"];
    NSString *str = [NSString stringWithFormat:@"http://app.sgone.cn/appapi_v4/wholesaler_common/get_private_storage_token"];
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSOperationQueue *queue = [NSOperationQueue mainQueue];
    
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (connectionError || data == nil)
        {
            return ;
        }
        //解析服务器返回来的json数据
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        
        NSString *token = [[dict objectForKey:@"data"] objectForKey:@"upToken"];
        NSNumber *expire = [[dict objectForKey:@"data"] objectForKey:@"expire"];
        
        
        self.QNtoken = token;
        
        //        NSLog(@"输出的responsedata是  %@",dict) ;
        
        if (token != nil)
        {
//            NSLog(@"-----------------%@商品头像获取七牛token成功：%@     %@",token,expire,dict);
            
            
            
            
        }
        
    }];
    
    
}


#pragma mark获取商品分类


-(void) getFenlei

{
    
    
    
    //    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    
    NSString *urlStr = [NSString stringWithFormat:@"%@product_category",SERVER_ADDRESS];
    //    NSDictionary *params = nil;
    [self.manager POST:urlStr parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"error_flag"] intValue] == 0) {
            
            
            //            NSLog(@"ttttt  %@",responseObject);
            
            //将数据传递到下一个方法里进行操作
            
            [self setupDataFormIdct:responseObject];
            
            
            //进行pickView以及蒙版的生成以及操作
            
            if (self.orderMskView == nil) {
                
                self.orderMskView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [UIScreen mainScreen].bounds.size.height  )];
            }
            self.orderMskView.backgroundColor = [UIColor clearColor];
            
            [self.view addSubview:self.orderMskView];
            //添加清除操作
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(fingerTapped:)];
            singleTap.cancelsTouchesInView = NO;
            [self.orderMskView addGestureRecognizer:singleTap];
            if (self.pickView1 == nil)
            {
                
                self.pickView1 = [[UIPickerView alloc]initWithFrame:CGRectMake(0,ViewHeight - 213, ViewWidth, 220)];
                
            }
            [self.pickView1 setTag:104];
            
            self.pickView1.backgroundColor = [UIColor whiteColor];
            self.pickView1.delegate = self;
            [self.orderMskView addSubview:self.pickView1];
            [self.pickView1 reloadAllComponents];
            
            
            
            self.pickView1.hidden = YES;
            self.orderMskView.hidden = YES;
            
            
        }
        
        
        else
        {
            
            [MBProgressHUD hideHUDForView:self.scrollView animated:YES];
            NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
            
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
            
            
            
            
        }
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error)
     {
         
         [MBProgressHUD hideHUDForView:self.scrollView animated:YES];
         UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请检查网络" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
         [alter show];
         
         
     }];
    
    
    
    
    
    
    
}


#pragma mark 移除maskView以及pickView的操作
-(void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer

{
    
    [self.pickView1 setHidden:YES];
    [self.orderMskView setHidden:YES];
    
    
    
    
}




#pragma mark 取消地址pickView的现实操作
-(void)pickViewHide:(UITapGestureRecognizer *)tap
{
    
    
    self.orderMskView2.hidden = YES;
    self.locatePicker.hidden = YES;
    
}





#pragma mark 分类获取网路数据处理
-(void)setupDataFormIdct:(NSDictionary*)dict
{
    
    
    
    //    NSLog(@"jjjjjjj %@",[dict[@"data"] class]);
    
    
    if ([dict[@"data"] isKindOfClass:[NSArray class]] ) {
        
        NSArray *menu = dict[@"data"];
        
        [menu enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
         
         {
             
             NSArray *menuArray = obj;
             
             Fruits1 *fruits1 = [[Fruits1 alloc]initWithDataDic:menuArray];
             
             [self.orderDatas addObject:fruits1];
             
         }];
        [self.pickView1 reloadAllComponents];
        
        //        [MBProgressHUD hideHUDForView:self.tableView animated:YES];
        
        
    }
    else
    {
        //        [MBProgressHUD hideHUDForView:self.tableView animated:YES];
        
//        NSLog(@"获取网络数据类型出错");
        
    }
    
    
    
}



#pragma mark pickViewDataSource协议方法






//指定pickView的组件个数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    
    if (pickerView.tag == 104) {
        return 2;
    }
    //其他PickView的操作
    else
    {
        
        
        
        return 1;
        
        
        
    }
    
    
    
}


//指定pickView中每一列的个数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{
    
    
    
    NSInteger selectIndeInCom0  = [pickerView selectedRowInComponent:0];
    
    Fruits1 *selectMenu = _orderDatas[selectIndeInCom0];

    
    if (pickerView.tag == 104 && component < _orderDatas.count)
    {
    
        return (component == 0) ?_orderDatas.count:selectMenu.fruits1.count;
            
        
    }
    else
    {
        
        return 2;
        
    }
    
    
}




//设置pickView的高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    
    if (pickerView.tag == 105) {
        
        
        return 35;
    }
    else
    {
        return 25;
    }
}






#pragma mark - UIPickerViewDelegate 协议方法
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{
    
    
    NSString *str1 = nil;
    
    if (pickerView.tag == 104) {
        
        if (component == 0) {
            Fruits1 *fruits12 = self.orderDatas[row];
            return fruits12.fruits1Name;
            
        }
        Fruits1 *selectMenu = self.orderDatas[self.currentSelectInCom0];
        Fruits2 *fruits22 = selectMenu.fruits1[row];
        return fruits22.fruits2Name;
        
        
    }
    
    else if (pickerView.tag == 105)

    {
        //需要网络判断是否是上架还是下架状态操作
        
        
        
        
        NSString *str = @"";
        str = [self.shangJiaArray objectAtIndex:row];
        
//        NSLog(@"uiuiuiuuisuduasd  %@",str);
        
        
        //判断上架，下架操作赋值
        if ([str isEqualToString:@"下架"]) {
            
            
            self.value = @"0";
       

        }
        else
        {
            
            self.value = @"1";
            
            
        }
        return str;

        
    }
    
    else
    {
    
        return str1;
    
    }
    
}



-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{
    
    if (pickerView.tag == 104) {
        if (component == 0) {
            
            _currentSelectInCom0 = row;
            [pickerView reloadComponent:1];
            [pickerView selectRow:0 inComponent:1 animated:YES];
            
            
        }
        



        //获取第一个数组选择的数据获取第二个数组选择的数据
        
        NSInteger selectIndexInCom0 = [self.pickView1 selectedRowInComponent:0];
        NSInteger selectIndexInCom1 = [self.pickView1 selectedRowInComponent:1];
        
        
        Fruits1 *select1 = [[Fruits1 alloc]init];
        Fruits2 *select2 = [[Fruits2 alloc]init];
        
        
        if (component == 0) {
            
            select1 = _orderDatas[selectIndexInCom0];
            select2 = select1.fruits1[selectIndexInCom1];
        }
        
        
        else if (component == 1) {
            
            select1 = _orderDatas[_currentSelectInCom0];
             select2 = select1.fruits1[selectIndexInCom1];
            [pickerView reloadComponent:0];

        }
        
        
        
        
        
        //将当前的数值放入到textFenlei中
        
        NSString *str = [NSString stringWithFormat:@"%@ %@",select1.fruits1Name,select2.fruits2Name];
        self.textFenlei.text = str;
        
        
        
        //确定子的ID号
        
        
        //用不着分类的ID
//        self.ID2 = [NSString stringWithFormat:@"%@",@(select2.menu2ID)];
//        NSLog(@"获得的子分类   %@ %@",self.textFenlei.text,@(select2.menu2ID));
        
        
        self.category = select1.fruits1Name;
        self.product = select2.fruits2Name;

        
        
    }
    
    
    
    else if (pickerView.tag == 105)
    {
        
        self.textShangjia.text = [self.shangJiaArray objectAtIndex:row];
        
        
    }
    

    
    
    
}




#pragma mark  - HZAreaPicker delegate
-(void)pickerDidChaneStatus:(HZAreaPickerView *)picker
{
    if (picker.pickerStyle == HZAreaPickerWithStateAndCityAndDistrict) {
        //传递省市到服务器上
        self.areaValue = [NSString stringWithFormat:@"%@ %@ %@", picker.locate.state, picker.locate.city, picker.locate.district];
        //        NSLog(@"66666  %@  %@  ",picker.locate.state ,picker.locate.city);
        
        
        
        //重新赋值
        self.stateValue = picker.locate.state;
        self.cityValue = picker.locate.city;
        
        
        //产地重新赋值
        self.textChandi.text = self.areaValue;
        //        NSLog(@"uiuiuououo  %@",self.textChandi.text);
        
        
        
        
#warning  刷新scrollView的数据
        //
        //        [self.scrollView];
        
        
    }
    
    
    
}




#pragma mark 选择图片传递到七牛服务器

-(void)imagePick:(id)sender
{
    
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选择", nil];
    
    [actionSheet showInView:self.scrollView];
    
}




- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    
    if (buttonIndex == 2) {
        
        return;
    }
    
    if(buttonIndex==1)
    {
        
        //调用系统相册
        _ctrl=[[UIImagePickerController alloc]init];
        _ctrl.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
        _ctrl.delegate=self;
        _ctrl.allowsEditing = YES;
        [self addChildViewController:_ctrl];
        self.tabBarController.tabBar.alpha = 0;
        [self.view addSubview:_ctrl.view];
        self.tabBarController.tabBar.hidden = YES;
        
        
        
        
    }
    else if (buttonIndex==0)
        
    {
        
        
        _ctrl=[[UIImagePickerController alloc]init];
        _ctrl.sourceType=UIImagePickerControllerSourceTypeCamera;
        _ctrl.delegate=self;
        _ctrl.allowsEditing = YES;
        
        [self addChildViewController:_ctrl];
        
        [self.view addSubview:_ctrl.view];
        
        
        
        
    }
}



#pragma mark - ImagePickerController Delegate
//点击取消按钮时候调用
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    
    if (_ctrl)
    {
        [_ctrl.view removeFromSuperview];
        self.navigationController.navigationBar.hidden=YES;
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
        
    }
}

//点击确定按钮
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    
    
    UIImage *image=[info objectForKey:UIImagePickerControllerEditedImage];
    
    
    self.imageView.image=image;
    
    
    
    self.filePath = [self.dataHandler dataFilePath:@"pifa.jpg"];
    
    NSData *webData = UIImageJPEGRepresentation(self.imageView.image, 0.5);
    [webData writeToFile:self.filePath atomically:YES];
    [self uploadImage];
    [picker.view removeFromSuperview];
    self.navigationController.navigationBar.hidden= YES;
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    
    
    
    
}


//网络上传到七牛的操作步骤以及属性
#pragma mark - upload qiniu
- (void)uploadImage{
    
    //获得的商品图片，需要传递给服务器
    self.nameKey = [NSString stringWithFormat:@"pf-%@-%@.jpg",self.dataHandler.phone, [self.dataHandler timeString]];
    
    //    NSLog(@"图片  %@    %@  %@",self.nameKey,self.dataHandler.phone,self.dataHandler.uToken);
    
    if (!self.uploader)
    {
        
        self.uploader = [[QNUploadManager alloc]init];
        
    }
    
    [MBProgressHUD showHUDAddedTo:self.scrollView animated:YES];
    [self.uploader putFile:_filePath key:self.nameKey token:self.QNtoken complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        
        [MBProgressHUD hideAllHUDsForView:self.scrollView animated:YES];
//        NSLog(@"%@", info);
//        NSLog(@"七牛返回信息%@", resp);
    } option:nil];
    
    
    
    
}


#pragma mark点击return时触发的事件,收回键盘

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    [UIView animateWithDuration:0.25 animations:^{
        if (self.view.y == -176) {
            self.view.y = 0;
            [textField resignFirstResponder];
        }
    }completion:^(BOOL finished){
        
        self.scrollView.scrollEnabled = YES;
        
        
    }];
    
    
    
    
    return YES;
    
}


//键盘上移操作

#pragma mark 起卖量键盘上移，再次点击键盘收回操作

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    
    if (    [textField isEqual:self.textphone] || [textField isEqual:self.textJunjia] || [textField isEqual:self.textPrice] || [textField isEqual:self.textNumber] || [textField isEqual:self.textUnit] || [textField isEqual:self.textUnit] || [textField isEqual:self.textHeavy])
    {
        [UIView animateWithDuration:0.25 animations:^{
            if (self.view.y == 0) {
                self.view.y = -176;
            }
        }completion:^(BOOL finished){
            //生成pickView
            
            
            self.scrollView.scrollEnabled = NO;
            
            
        }];
        
        
    }
    
    return YES;
}






#pragma mark 键盘上移操作，textviewdelegat操作

//textview上移操作


- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    
    //当textView进行编辑的时候整个view进行上移
    // if (!iPhone5)
    
    
    [UIView animateWithDuration:0.25 animations:^{
        if (self.view.y == 0) {
            self.view.y = -176;
        }
    }completion:^(BOOL finished){
        self.scrollView.scrollEnabled = NO;
    }];
    
    return YES;
    
    
}

//键盘输入数字操作

//判断是否输入的为数字
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs;
    if( [self.textphone isFirstResponder]  || [self.textPrice isFirstResponder]  || [self.textJunjia isFirstResponder])
    {
        cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL basicTest = [string isEqualToString:filtered];
        if(!basicTest)
        {
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"请输入数字"
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
            
            [alert show];
            
            //    [self.view makeToast:@"请输入数字"];
            return NO;
        }
    }
    
    
    
    
    //其他的类型不需要检测，直接写入
    return YES;
}







//收回键盘操作,整个界面复原
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        
        
        [UIView animateWithDuration:0.25 animations:^{
            if (self.view.y == -176) {
                self.view.y = 0;
                
            }
        }completion:^(BOOL finished){
            [textView resignFirstResponder];
            self.scrollView.scrollEnabled = YES;
            
            
        }];
        
        return NO;
    }
    
    
    return YES;
    
}



-(void)textViewDidChange:(UITextView *)textView
{
    
    
    //给textView输入提示操作
    if ([textView.text length] == 0) {
        [self.placeholder setHidden:NO];
    }else
    {
        [self.placeholder setHidden:YES];
        
    }
    
    
    
    
    
}









@end
